package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.SetmealService;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * @Author: Sabrina
 * @Date:2025/06/13/15:39
 * @Description:
 */
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    /**
     * 添加商品到购物车
     * @param dto
     */
    @Transactional
    @Override
    public void addCart(ShoppingCartDTO dto) {
        //userId     name image amount      number=1 createTime
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(dto,shoppingCart);

        //当前微信登录用户
        shoppingCart.setUserId(BaseContext.getCurrentId());

        //判断当前商品是否已经存在于购物车中:
        //1 不存在,数量 = 1
        //2 存在,原本的取出来 + 1
        List<ShoppingCart> shoppingCartList =  shoppingCartMapper.getCart(shoppingCart);//里面只有一条数据,用集合是为了复用性
        if (shoppingCartList != null &&shoppingCartList.size() > 0 ){
            //存在
            shoppingCart = shoppingCartList.get(0);
            shoppingCart.setNumber(shoppingCart.getNumber()+1);
            shoppingCartMapper.update(shoppingCart);
            //存在就不用设置图片等信息了
        }else {
            //不存在,设置为1
            if (dto.getDishId() != null){
                //添加的是菜品,查菜品表
                Dish dish = dishMapper.getById(dto.getDishId());
                shoppingCart.setName(dish.getName());
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setImage(dish.getImage());
                //取出来name,image,price(shopping_cart表里用的单词是amount)放进购物车表中
            }else{
                Setmeal setmeal = setmealMapper.getById(dto.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setImage(setmeal.getImage());
            }
            //数量1,时间
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.addCart(shoppingCart);
        }
    }

    /**
     * 查看购物车数据
     * @return
     */
    @Override
    public List<ShoppingCart> getCategories() {
        return shoppingCartMapper.getCart(new ShoppingCart().builder().userId(BaseContext.getCurrentId()).build());
    }

    /**
     * 清空购物车
     */
    @Override
    public void deleteCart() {
        shoppingCartMapper.clean(BaseContext.getCurrentId());
    }

    /**
     * 删除购物车中的一条数据
     * @param dto
     */
    @Transactional
    @Override
    public void deleteOneProduct(ShoppingCartDTO dto) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(dto,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> list = shoppingCartMapper.getCart(shoppingCart);

        shoppingCart = list.get(0);
        Integer number = shoppingCart.getNumber();
        if (number == 1){
            shoppingCartMapper.delete(shoppingCart);
        }else {
            shoppingCart.setNumber(shoppingCart.getNumber() - 1);
            shoppingCartMapper.update(shoppingCart);
        }
    }
}
